// 热电偶相关
package rdo

import (
	"bufio"
	"bytes"
	"io"
	"strconv"
	"strings"
)

//热电偶数组，索引号为温度值℃，数据项为电压值mV
var rdoVa []int = make([]int, 0, 1400)

//加载热电偶
func init() {
	loadRdoCsv_str()
}

//二分法在dats数组中查找target值，返回值：查找次数,最接近目标值的数据下标,最接近的目标值,是否精确找到了目标值
func binarySearch(dats []int, target int) (int, int, int, bool) {
	var l, m, r int = 0, 0, len(dats) - 1    //左、中、右下标
	var lv, mv, rv int = dats[0], 0, dats[r] //左、中、右数值
	var c int                                //下标差，差值
	if target > rv {
		return 0, r, rv, false
	} else if target < lv {
		return 0, l, lv, false
	}
	for i := 0; ; i++ {
		c = r - l
		m = l + c/2
		mv, lv, rv = dats[m], dats[l], dats[r]
		if c <= 1 {
			if c > 0 {
				return i, l, lv, false
			} else {
				return i, r, rv, false
			}
		}
		switch {
		case lv == target:
			return i, l, target, true
		case rv == target:
			return i, r, target, true
		case mv == target:
			return i, m, target, true
		case mv > target:
			r = m
		case mv < target:
			l = m
		}
	}
}

//从RDO_DAT_CSV中的字符串中读取热电偶数据表
func loadRdoCsv_str() {
	r := bufio.NewReader(bytes.NewReader([]byte(rRDO_DAT_CSV)))
	i := 0
	for ; ; i++ {
		l, e := r.ReadString('\n')
		if e != nil || e == io.EOF {
			break
		}
		a := strings.Split(l, ",")
		wd, e1 := strconv.Atoi(strings.TrimSpace(a[0]))
		if wd != i || e1 != nil {
			break
		}
		ds, e2 := strconv.Atoi(strings.TrimSpace(a[1]))
		if e2 != nil {
			break
		}
		rdoVa = append(rdoVa, ds)
	}
}

//线性插值查表法，依据热电偶数据表，根据电压值mVy按线性插值法计算热电偶测量的温度值
//mVy测量电压值，返回温度值，单位为0.1℃
func CalcRdoWd(mVy int) int {
	_, wd, mV, ok := binarySearch(rdoVa, mVy)
	var wdx int
	switch {
	case ok:
		wdx = wd * 10
	case wd == 0, wd == len(rdoVa)-1:
		wdx = wd * 10
	default:
		aDy, bDy := mVy-mV, rdoVa[wd+1]-mV
		awd := float32(aDy) / float32(bDy)
		wdx = wd*10 + int(awd*10)
		// log.Println("rdo calc", mVy, wd, aDy, bDy, awd, wdx)
	}
	return wdx
}

//热电偶数据表，温度【℃】,热电偶电压值【mV】
const rRDO_DAT_CSV = `0,0
1,0
2,39
3,79
4,119
5,158
6,198
7,238
8,277
9,317
10,357
11,397
12,437
13,477
14,517
15,557
16,597
17,637
18,677
19,718
20,758
21,798
22,838
23,879
24,919
25,960
26,1000
27,1041
28,1081
29,1122
30,1136
31,1203
32,1244
33,1285
34,1326
35,1366
36,1407
37,1448
38,1489
39,1530
40,1571
41,1612
42,1653
43,1694
44,1735
45,1776
46,1817
47,1858
48,1899
49,1941
50,1982
51,2023
52,2064
53,2106
54,2147
55,2188
56,2230
57,2271
58,2312
59,2354
60,2395
61,2436
62,2478
63,2519
64,2561
65,2602
66,2644
67,2685
68,2727
69,2768
70,2810
71,2851
72,2893
73,2934
74,2976
75,3017
76,3059
77,3100
78,3142
79,3184
80,3225
81,3267
82,3308
83,3350
84,3391
85,3433
86,3474
87,3516
88,3557
89,3599
90,3640
91,3682
92,3723
93,3765
94,3806
95,3848
96,3889
97,3931
98,3972
99,4013
100,4054
101,4096
102,4138
103,4179
104,4220
105,4262
106,4303
107,4344
108,4385
109,4427
110,4468
111,4509
112,4550
113,4591
114,4633
115,4674
116,4715
117,4756
118,4797
119,4838
120,4879
121,4920
122,4961
123,5002
124,5043
125,5084
126,5124
127,5165
128,5206
129,5247
130,5288
131,5328
132,5369
133,5410
134,5450
135,5491
136,5532
137,5572
138,5613
139,5653
140,5694
141,5735
142,5775
143,5815
144,5856
145,5896
146,5937
147,5977
148,6017
149,6058
150,6098
151,6138
152,6179
153,6219
154,6259
155,6299
156,6339
157,6380
158,6420
159,6460
160,6500
161,6540
162,6580
163,6620
164,6660
165,6701
166,6741
167,6781
168,6821
169,6861
170,6901
171,6941
172,6981
173,7021
174,7060
175,7100
176,7140
177,7180
178,7220
179,7260
180,7300
181,7340
182,7380
183,7420
184,7460
185,7500
186,7540
187,7579
188,7619
189,7659
190,7699
191,7739
192,7779
193,7819
194,7859
195,7899
196,7939
197,7979
198,8019
199,8058
200,8099
201,8138
202,8178
203,8218
204,8258
205,8298
206,8338
207,8378
208,8418
209,8458
210,8499
211,8539
212,8579
213,8619
214,8659
215,8699
216,8739
217,8779
218,8819
219,8860
220,8900
221,8940
222,8980
223,9020
224,9061
225,9101
226,9141
227,9181
228,9222
229,9262
230,9302
231,9343
232,9383
233,9423
234,9464
235,9504
236,9545
237,9585
238,9626
239,9666
240,9707
241,9747
242,9788
243,9828
244,9869
245,9909
246,9950
247,9991
248,10031
249,10072
250,10113
251,10153
252,10194
253,10235
254,10276
255,10316
256,10357
257,10398
258,10439
259,10480
260,10520
261,10561
262,10602
263,10643
264,10684
265,10725
266,10766
267,10807
268,10848
269,10889
270,10930
271,10971
272,11012
273,11053
274,11094
275,11135
276,11176
277,11217
278,11259
279,11300
280,11341
281,11382
282,11423
283,11465
284,11506
285,11547
286,11588
287,11630
288,11671
289,11712
290,11753
291,11795
292,11836
293,11877
294,11919
295,11960
296,12001
297,12043
298,12084
299,12126
300,12167
301,12209
302,12250
303,12291
304,12333
305,12374
306,12416
307,12457
308,12499
309,12540
310,12582
311,12624
312,12665
313,12707
314,12748
315,12790
316,12831
317,12873
318,12915
319,12956
320,12998
321,13040
322,13081
323,13123
324,13165
325,13206
326,13248
327,13290
328,13331
329,13373
330,13415
331,13457
332,13498
333,13540
334,13582
335,13624
336,13665
337,13707
338,13749
339,13791
340,13833
341,13874
342,13916
343,13958
344,14000
345,14042
346,14084
347,14126
348,14167
349,14209
350,14251
351,14293
352,14335
353,14377
354,14419
355,14461
356,14503
357,14545
358,14587
359,14629
360,14671
361,14713
362,14755
363,14797
364,14839
365,14881
366,14923
367,14965
368,15007
369,15049
370,15091
371,15133
372,15175
373,15217
374,15259
375,15301
376,15343
377,15385
378,15427
379,15469
380,15511
381,15554
382,15596
383,15638
384,15680
385,15722
386,15764
387,15806
388,15849
389,15891
390,15933
391,15975
392,16071
393,16059
394,16102
395,16143
396,16186
397,16228
398,16270
399,16312
400,16355
401,16397
402,16439
403,16482
404,16524
405,16566
406,16608
407,16651
408,16693
409,16735
410,16778
411,16820
412,16862
413,16904
414,16947
415,16989
416,17031
417,17074
418,17116
419,17158
420,17201
421,17243
422,17285
423,17328
424,17370
425,17413
426,17455
427,17497
428,17540
429,17582
430,17624
431,17667
432,17709
433,17752
434,17794
435,17837
436,17879
437,17921
438,17964
439,18006
440,18049
441,18091
442,18134
443,18176
444,18218
445,18261
446,18303
447,18346
448,18388
449,18431
450,18473
451,18516
452,18558
453,18601
454,18643
455,18686
456,18728
457,18771
458,18813
459,18856
460,18898
461,18941
462,18983
463,19026
464,19068
465,19111
466,19154
467,19196
468,19239
469,19281
470,19324
471,19366
472,19409
473,19451
474,19494
475,19537
476,19579
477,19622
478,19664
479,19707
480,19750
481,19792
482,19835
483,19877
484,19920
485,19962
486,20005
487,20048
488,20090
489,20133
490,20175
491,20218
492,20261
493,20303
494,20346
495,20389
496,20431
497,20474
498,20516
499,20559
500,20602
501,20644
502,20687
503,20730
504,20772
505,20815
506,20857
507,20900
508,20943
509,20985
510,21028
511,21071
512,21113
513,21156
514,21199
515,21241
516,21284
517,21326
518,21369
519,21412
520,21454
521,21497
522,21540
523,21582
524,21625
525,21668
526,21710
527,21753
528,21796
529,21838
530,21881
531,21924
532,21966
533,22009
534,22052
535,22094
536,22137
537,22179
538,22222
539,22265
540,22307
541,22350
542,22393
543,22435
544,22478
545,22521
546,22563
547,22606
548,22649
549,22691
550,22734
551,22776
552,22819
553,22862
554,22904
555,22947
556,22990
557,23032
558,23075
559,23117
560,23160
561,23203
562,23245
563,23288
564,23331
565,23373
566,23416
567,23458
568,23501
569,23544
570,23586
571,23629
572,23671
573,23714
574,23757
575,23799
576,23842
577,23884
578,23927
579,23970
580,24012
581,24055
582,24097
583,24140
584,24182
585,24225
586,24267
587,24310
588,24353
589,24395
590,24438
591,24480
592,24523
593,24565
594,24608
595,24650
596,24693
597,24735
598,24778
599,24820
600,24863
601,24905
602,24948
603,24990
604,25033
605,25075
606,25118
607,25160
608,25203
609,25245
610,25288
611,25330
612,25373
613,25415
614,25458
615,25500
616,25543
617,25585
618,25627
619,25670
620,25712
621,25755
622,25797
623,25840
624,25882
625,25924
626,25967
627,26009
628,26052
629,26094
630,26136
631,26179
632,26221
633,26263
634,26306
635,26348
636,26390
637,26433
638,26475
639,26517
640,26560
641,26602
642,26644
643,26687
644,26729
645,26771
646,26814
647,26856
648,26898
649,26940
650,26983
651,27025
652,27067
653,27109
654,27152
655,27194
656,27236
657,27278
658,27320
659,27363
660,27405
661,27447
662,27489
663,27531
664,27574
665,27616
666,27658
667,27700
668,27742
669,27784
670,27826
671,27869
672,27911
673,27953
674,27995
675,28037
676,28079
677,28121
678,28163
679,28205
680,28247
681,28289
682,28332
683,28374
684,28416
685,28458
686,28500
687,28542
688,28584
689,28626
690,28668
691,28710
692,28752
693,28794
694,28835
695,28877
696,28919
697,28961
698,29003
699,29045
700,29087
701,29129
702,29171
703,29213
704,29255
705,29297
706,29338
707,29380
708,29422
709,29464
710,29506
711,29548
712,29589
713,29631
714,29673
715,29715
716,29757
717,29798
718,29840
719,29882
720,29924
721,29965
722,30007
723,30049
724,30090
725,30132
726,30174
727,30216
728,30257
729,30299
730,30341
731,30382
732,30424
733,30466
734,30507
735,30549
736,30590
737,30632
738,30674
739,30715
740,30757
741,30798
742,30840
743,30881
744,30923
745,30964
746,31006
747,31047
748,31089
749,31130
750,31172
751,31213
752,31255
753,31296
754,31338
755,31379
756,31421
757,31462
758,31504
759,31545
760,31586
761,31628
762,31699
763,31710
764,31752
765,31793
766,31834
767,31876
768,31917
769,31958
770,32000
771,32040
772,32082
773,32124
774,32165
775,32206
776,32247
777,32289
778,32330
779,32371
780,32412
781,32453
782,32497
783,32536
784,32577
785,32618
786,32659
787,32700
788,32741
789,32783
790,32824
791,32865
792,32906
793,32947
794,32988
795,33029
796,33070
797,33111
798,33152
799,33193
800,33234
801,33275
802,33316
803,33357
804,33398
805,33439
806,33480
807,33521
808,33562
809,33602
810,33644
811,33685
812,33726
813,33767
814,33808
815,33848
816,33889
817,33930
818,33971
819,34012
820,34053
821,34093
822,34134
823,34175
824,34216
825,34257
826,34297
827,34338
828,34379
829,34420
830,34460
831,34501
832,34542
833,34582
834,34623
835,34664
836,34704
837,34745
838,34786
839,34826
840,34867
841,34908
842,34948
843,34989
844,35029
845,35070
846,35110
847,35151
848,35192
849,35232
850,35273
851,35313
852,35354
853,35394
854,35435
855,35475
856,35516
857,35556
858,35596
859,35637
860,35677
861,35718
862,35758
863,35798
864,35839
865,35879
866,35920
867,35960
868,36000
869,36041
870,36081
871,36121
872,36162
873,36202
874,36242
875,36282
876,36323
877,36363
878,36403
879,36443
880,36484
881,36524
882,36564
883,36604
884,36644
885,36685
886,36725
887,36765
888,36805
889,36845
890,36885
891,36925
892,36965
893,37006
894,37046
895,37086
896,37126
897,37166
898,37206
899,37246
900,37286
901,37326
902,37366
903,37406
904,37446
905,37486
906,37526
907,37566
908,37606
909,37646
910,37686
911,37725
912,37765
913,37805
914,37845
915,37885
916,37925
917,37965
918,38005
919,38044
920,38084
921,38124
922,38164
923,38204
924,38243
925,38283
926,38323
927,38363
928,38402
929,38442
930,38482
931,38522
932,38561
933,38601
934,38641
935,38680
936,38720
937,38760
938,38799
939,38839
940,38878
941,38918
942,38958
943,38997
944,39037
945,39076
946,39116
947,39155
948,39195
949,39235
950,39274
951,39314
952,39353
953,39393
954,39432
955,39471
956,39511
957,39550
958,39590
959,39629
960,39669
961,39708
962,39747
963,39787
964,39826
965,39866
966,39905
967,39944
968,39984
969,40023
970,40062
971,40101
972,40141
973,40180
974,40219
975,40259
976,40298
977,40337
978,40376
979,40415
980,40455
981,40494
982,40533
983,40572
984,40611
985,40651
986,40690
987,40729
988,40768
989,40807
990,40846
991,40885
992,40924
993,40963
994,41002
995,41042
996,41081
997,41120
998,41159
999,41198
1000,41237
1001,41276
1002,41315
1003,41354
1004,41393
1005,41431
1006,41470
1007,41509
1008,41548
1009,41587
1010,41626
1011,41665
1012,41704
1013,41743
1014,41781
1015,41820
1016,0
1017,41898
1018,41937
1019,41976
1020,42014
1021,42053
1022,42092
1023,42131
1024,42169
1025,42208
1026,42247
1027,42286
1028,42324
1029,42363
1030,42402
1031,42440
1032,42479
1033,42518
1034,42556
1035,42595
1036,42633
1037,42672
1038,42711
1039,42749
1040,42788
1041,42826
1042,42865
1043,42903
1044,42942
1045,42980
1046,43019
1047,43057
1048,43096
1049,43134
1050,43173
1051,43211
1052,43250
1053,43288
1054,43327
1055,43365
1056,43403
1057,43442
1058,43480
1059,43518
1060,43557
1061,43595
1062,43633
1063,43672
1064,43710
1065,43748
1066,43787
1067,43825
1068,43863
1069,43901
1070,43940
1071,43978
1072,44016
1073,44054
1074,44092
1075,44130
1076,44169
1077,44207
1078,44245
1079,44283
1080,44321
1081,44359
1082,44397
1083,44435
1084,44473
1085,44512
1086,44550
1087,44588
1088,44626
1089,44664
1090,44702
1091,44740
1092,44778
1093,44816
1094,44853
1095,44891
1096,44929
1097,44967
1098,45005
1099,45043
1100,45081
1101,45119
1102,45157
1103,45194
1104,45232
1105,45270
1106,45308
1107,45346
1108,45383
1109,45421
1110,45459
1111,45497
1112,45534
1113,45572
1114,45610
1115,45647
1116,45685
1117,45723
1118,45760
1119,45798
1120,45836
1121,45873
1122,45911
1123,45948
1124,45986
1125,46024
1126,46061
1127,46099
1128,46136
1129,46174
1130,46211
1131,46249
1132,46286
1133,46324
1134,46361
1135,46398
1136,46436
1137,46473
1138,46511
1139,46548
1140,46585
1141,46623
1142,46660
1143,46697
1144,46735
1145,46772
1146,46809
1147,46847
1148,46884
1149,46921
1150,46958
1151,46995
1152,47033
1153,47070
1154,47107
1155,47144
1156,47181
1157,47218
1158,47256
1159,47293
1160,47330
1161,47367
1162,47404
1163,47441
1164,47478
1165,47515
1166,47552
1167,47589
1168,47626
1169,47663
1170,47700
1171,47737
1172,47774
1173,47811
1174,47848
1175,47884
1176,47921
1177,47958
1178,47995
1179,48032
1180,48069
1181,48105
1182,48142
1183,48179
1184,48216
1185,48252
1186,48289
1187,48326
1188,48363
1189,48399
1190,48436
1191,48473
1192,48509
1193,48546
1194,48582
1195,48619
1196,48656
1197,48692
1198,48729
1199,48765
1200,48802
1201,48838
1202,48875
1203,48911
1204,48948
1205,48984
1206,49021
1207,49057
1208,49093
1209,49130
1210,49166
1211,49202
1212,49239
1213,49275
1214,49311
1215,49348
1216,49384
1217,49420
1218,49456
1219,49493
1220,49529
1221,49565
1222,49606
1223,49637
1224,49674
1225,49710
1226,49746
1227,49782
1228,49818
1229,49854
1230,49890
1231,49926
1232,49962
1233,49998
1234,50034
1235,50070
1236,50106
1237,50142
1238,50178
1239,50214
1240,50250
1241,50286
1242,50322
1243,50358
1244,50393
1245,50429
1246,50465
1247,50501
1248,50537
1249,50572
1250,50608
1251,50644
1252,50680
1253,50715
1254,50751
1255,50787
1256,50822
1257,50858
1258,50894
1259,50929
1260,50965
1261,51000
1262,51036
1263,51071
1264,51107
1265,51142
1266,51178
1267,51213
1268,51249
1269,51284
1270,51320
1271,51355
1272,51391
1273,51426
1274,51461
1275,51497
1276,51532
1277,51567
1278,51603
1279,51638
1280,51673
1281,51708
1282,51744
1283,51779
1284,51814
1285,51849
1286,51885
1287,51920
1288,51955
1289,51990
1290,52025
1291,52060
1292,52095
1293,52130
1294,52165
1295,52200
1296,52235
1297,52270
1298,52305
1299,52340
1300,52375
1301,52410
1302,52445
1303,52480
1304,52515
1305,52550
1306,52585
1307,52620
1308,52654
1309,52689
1310,52724
1311,52759
1312,52794
1313,52828
1314,52863
1315,52898
1316,52932
1317,52967
1318,53002
1319,53037
1320,53071
1321,53106
1322,53140
1323,53175
1324,53210
1325,53244
1326,53279
1327,53313
1328,53348
1329,53382
1330,53417
1331,53451
1332,53486
1333,53520
1334,53555
1335,53589
1336,53623
1337,53658
1338,53692
1339,53727
1340,53761
1341,53795
1342,53830
1343,53864
1344,53898
1345,53932
1346,53967
1347,54001
1348,54035
1349,54069
1350,54104
1351,54138
1352,54172
1353,54206
1354,54240
1355,54274
1356,54308
1357,54343
1358,54377
1359,54411
1360,54445
1361,54479
1362,54513
1363,54547
1364,54581
1365,54615
1366,54649
1367,54683
1368,54717
1369,54751
1370,54785
1371,54819
1372,54852
1373,54886
`
